﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class ProduceOptionObject
    {
        public static ProduceOption[] GetProduceOptions()
        {
            return Query(null);
        }

        public static ProduceOption GetProduceOption(int id)
        {
            ProduceOption produceOption = new ProduceOption();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProduceOptionDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = ProduceOptionDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable produceOptions = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            produceOption.Id                              = Convert.ToInt32(produceOptions.Rows[0][ProduceOptionDesc.Id]);
            produceOption.ProductOptionCagegoryId         = Convert.ToInt32(produceOptions.Rows[0][ProduceOptionDesc.ProductOptionCagegoryId]);
            produceOption.Name                            = Convert.ToString(produceOptions.Rows[0][ProduceOptionDesc.Name]);

            return produceOption;
        }

        public static ProduceOption GetProduceOption(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProduceOptionDesc.Name, ConditionExpress.EQUALS, name);

            ProduceOption[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static ProduceOption[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + ProduceOptionDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                ProduceOption[] produceOptions = new ProduceOption[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ProduceOption produceOption = new ProduceOption();

                    produceOption.Id                                = Convert.ToInt32(table.Rows[i][ProduceOptionDesc.Id]);
                    produceOption.ProductOptionCagegoryId           = Convert.ToInt32(table.Rows[i][ProduceOptionDesc.ProductOptionCagegoryId]);
                    produceOption.Name                              = Convert.ToString(table.Rows[i][ProduceOptionDesc.Name]);

                    produceOptions.SetValue(produceOption, i);
                }

                return produceOptions;
            }
            return null;
        }

        public static Int32 Add(ProduceOption produceOption)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(ProduceOptionDesc.ProductOptionCagegoryId,              produceOption.ProductOptionCagegoryId);
            builder.InsertAdd(ProduceOptionDesc.Name,                                 produceOption.Name);

            builder.QueryTableName = ProduceOptionDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProduceOptionDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = ProduceOptionDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(ProduceOption produceOption)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(ProduceOptionDesc.ProductOptionCagegoryId,         produceOption.ProductOptionCagegoryId);
            builder.UpdateAdd(ProduceOptionDesc.Name,                            produceOption.Name);

            builder.ConditionAdd(ProduceOptionDesc.Id, ConditionExpress.EQUALS, produceOption.Id);
            builder.QueryTableName = ProduceOptionDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
